package dfrz.com.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author PuXiaLing
 * @description: TODO
 * @date 2024/1/5 19:39
 */
public class HttpUtil {
    public static String getParmater(HttpServletRequest request){
        //普通数据
//        String loginName=req.getParameter("loginName");
//        String loginPassword=req.getParameter("loginPassword");
//        System.out.println("收到数据");
        //Json数据
        //缓冲字符流读取json数据

        try {
            InputStream is = null;
            is = request.getInputStream();
            InputStreamReader isr = new InputStreamReader(is,"utf-8");
            BufferedReader bf =new BufferedReader(isr);
            String parameters = null;
            StringBuilder builder = new StringBuilder();
            while ((parameters = bf.readLine())!=null){
                builder.append(parameters);
            }

            return builder.toString();
        } catch (IOException e) {
             e.printStackTrace();
        }
        return null;
    }

    public static void responseResult(Object data,HttpServletResponse response){
        try {
            String json = JSON.toJSONString(data);
            response.getWriter().println(json);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
